package server

import (
	"context"
	"errors"
	"srv/internal/dao"
	"srv/pkg"
	pb "srv/proto"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	pb.UnimplementedUserServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) UserRegister(_ context.Context, in *pb.UserRegisterReq) (*pb.UserRegisterResp, error) {
	u := dao.User{
		Username: in.Username,
		Password: pkg.Md5Pwd(in.Password),
	}
	id, message := u.UserRegister()
	if message != "" {
		return &pb.UserRegisterResp{Id: int64(id)}, errors.New(message)
	}
	return &pb.UserRegisterResp{Id: int64(id)}, nil
}

// 视频发布
func (s *Server) VideoCreate(_ context.Context, in *pb.VideoCreateReq) (*pb.VideoCreateResp, error) {
	v := dao.Video{
		UserId:           int(in.UserId),
		VideoUrl:         in.VideoUrl,
		VideoCover:       in.VideoCover,
		VideoStatus:      int8(in.VideoStatus),
		VideoAdoptStatus: int8(in.VideoAdoptStatus),
		Type:             int8(in.Type),
	}
	id, message := v.VideoCreate()
	if message != "" {
		return &pb.VideoCreateResp{Id: int64(id)}, errors.New(message)
	}
	return &pb.VideoCreateResp{Id: int64(id)}, nil
}

// 视频列表
func (s *Server) VideoList(_ context.Context, in *pb.VideoListReq) (*pb.VideoListResp, error) {
	var v dao.Video
	list, message := v.VideoList(int(in.Page), int(in.Size))
	if message != "" {
		return &pb.VideoListResp{List: list}, errors.New(message)
	}
	return &pb.VideoListResp{List: list}, nil
}

// 个人主页
func (s *Server) UserShow(_ context.Context, in *pb.UserShowReq) (*pb.UserShowResp, error) {
	var v dao.Video
	list, message := v.UserShow(int(in.Id))
	if message != "" {
		return &pb.UserShowResp{List: list}, errors.New(message)
	}
	return &pb.UserShowResp{List: list}, nil
}
